﻿namespace FFF.Test.Entity.FW.View
{
    using System;
    using System.Collections.Generic;
    using System.ComponentModel.DataAnnotations;
    using System.ComponentModel.DataAnnotations.Schema;
    using System.Linq;
    using Abp.AutoMapper;
    using Ydm.Extensions;
    using Abp.Domain.Entities;
    using Microsoft.EntityFrameworkCore;
    using Microsoft.EntityFrameworkCore.Metadata.Builders;

    /// <summary>
    /// 用户表视图
    /// </summary>
    public partial class ViewFWUser
    {
        /// <summary>
        /// 用户的权限点
        /// </summary>
        [NotMapped]
        public List<ViewFWAction2User> Action2Users { get; set; }

        /// <summary>
        /// 过滤拥有的系统
        /// </summary>
        [NotMapped]
        public List<Guid> AplicationGUIDs { get; set; }

        /// <summary>
        /// 用户有的模块GUIDs
        /// </summary>
        [NotMapped]
        public List<Guid> ModuleGUIDs { get; set; }

        /// <summary>
        /// 用户拥有的功能的GUId的集合
        /// </summary>
        [NotMapped]
        public List<Guid> FunctionGUIDs { get; set; }

        /// <summary>
        /// 是否有这个动作点的权限
        /// </summary>
        /// <param name="functionCode">功能code</param>
        /// <param name="actionCode">动作点code</param>
        /// <returns>是否有权限</returns>
        public bool IsHaveRights(string functionCode, string actionCode)
        {
            if (this.UserIsAdmin == 1)
            {
                return true;
            }

            if (functionCode.IsNullOrEmpty() || actionCode.IsNullOrEmpty())
            {
                return false;
            }

            return this.Action2Users.Any(x => x.FunctionCode.ToLower().Contains(functionCode.ToLower()) && x.ActionCode == actionCode);
        }
    }
}
